package view.collection.tree;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import view.common.JTextFieldLimit;
import controller.BSTController;
import dataStructure.DataStructureApplet;

/**
 *
 */
public class BSTButtonsPanel extends JPanel {
	private static final long serialVersionUID = 5778728138331331309L;

	private BSTController controller;
	private static final int PARAMETER_MAX = 10;
	private static final int MAX_INPUT_LENGTH = 3;

	/** Creates new form BSTButtonsPanel */
	public BSTButtonsPanel() {
		initComponents();
	}

	void addController(BSTController controller) {
		this.controller = controller;
	}

	public void setWithChangeParameter(boolean withChangeParameter,
			String parameterName) {
		if (withChangeParameter) {
			for (int i = 1; i < PARAMETER_MAX; i++)
				parameterCombo.addItem(new Integer(i));
		}
		parameterLabel.setVisible(withChangeParameter);
		parameterCombo.setVisible(withChangeParameter);

		parameterLabel.setText(parameterName);

		parameterCombo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				parameterButtonActionPerformed(evt);
			}
		});
	}

	public void setParameter(int parameter) {
		parameterCombo.setSelectedItem(parameter);
	}

	private String getText() {
		String text = textField.getText();

		textField.grabFocus();
		textField.selectAll();
		return text;
	}

	/**
	 * Habilita o deshabilita todos los controles del panel
	 * 
	 * @param enable
	 */
	public void enableComponents(boolean enable) {
		for (Component component : this.getComponents()) {
			component.setEnabled(enable);
		}

		this.traverseButton.setEnabled(enable && !controller.isTreeEmpty());
		this.removeButton.setEnabled(enable && !controller.isTreeEmpty());
		this.clearButton.setEnabled(enable && !controller.isTreeEmpty());
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		// setBackground(new java.awt.Color(255, 255, 255));
		setBorder(BorderFactory.createTitledBorder("Control: Árbol binario"));
		// setBorder(javax.swing.BorderFactory.createLineBorder(new
		// java.awt.Color(0, 0, 0)));
		// setPreferredSize(new java.awt.Dimension(840, 32));

		jLabel1 = new JLabel("(" + BSTController.MIN_VALUE + " a "
				+ BSTController.MAX_VALUE + ")");
		jLabel1.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));

		textField = new JTextField();
		textField.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
		textField.setDocument(new JTextFieldLimit(MAX_INPUT_LENGTH));
		textField.setColumns(MAX_INPUT_LENGTH);
		textField.putClientProperty("JComponent.sizeVariant", "large");

		Icon addIcon = new ImageIcon(DataStructureApplet.BUTTON_ADD_IMAGE);
		insertButton = new JButton("Insertar", addIcon);
		insertButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		insertButton.setHorizontalTextPosition(SwingConstants.CENTER);
		insertButton.putClientProperty("JComponent.sizeVariant", "small");
		insertButton.setToolTipText("Insertar elemento en el árbol");
		insertButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
		insertButton.setMnemonic(KeyEvent.VK_E);
		insertButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				insertButtonActionPerformed(evt);
			}
		});

		Icon deleteIcon = new ImageIcon(DataStructureApplet.BUTTON_DELETE_IMAGE);
		removeButton = new JButton("Eliminar", deleteIcon);
		removeButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		removeButton.setHorizontalTextPosition(SwingConstants.CENTER);
		removeButton.putClientProperty("JComponent.sizeVariant", "small");
		removeButton.setToolTipText("Eliminar elemento del árbol");
		removeButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
		removeButton.setMnemonic(KeyEvent.VK_E);
		removeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				removeButtonActionPerformed(evt);
			}
		});

		Icon traverseIcon = new ImageIcon(
				DataStructureApplet.BUTTON_TRAVERSE_IMAGE);
		traverseButton = new JButton("Recorrer", traverseIcon);
		traverseButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		traverseButton.setHorizontalTextPosition(SwingConstants.CENTER);
		traverseButton.putClientProperty("JComponent.sizeVariant", "small");
		traverseButton.setToolTipText("Recorrer árbol");
		traverseButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
		traverseButton.setMnemonic(KeyEvent.VK_C);
		traverseButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				traverseButtonActionPerformed(evt);
			}
		});

		traverseOption = new JComboBox();
		traverseOption.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "In-Order", "Pre-Order", "Post-Order" }));
		traverseOption.putClientProperty("JComponent.sizeVariant", "large");

		Icon cleanIcon = new ImageIcon(DataStructureApplet.BUTTON_REFRESH_IMAGE);
		clearButton = new JButton("Vaciar", cleanIcon);
		clearButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		clearButton.setHorizontalTextPosition(SwingConstants.CENTER);
		clearButton.putClientProperty("JComponent.sizeVariant", "small");
		clearButton.setToolTipText("Vaciar árbol");
		clearButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
		clearButton.setMnemonic(KeyEvent.VK_V);
		clearButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				clearButtonActionPerformed(evt);
			}
		});

		Icon randomIcon = new ImageIcon(DataStructureApplet.BUTTON_RANDOM_IMAGE);
		insertRandomButton = new JButton("Random", randomIcon);
		insertRandomButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		insertRandomButton.setHorizontalTextPosition(SwingConstants.CENTER);
		insertRandomButton.putClientProperty("JComponent.sizeVariant", "small");
		insertRandomButton.setToolTipText("Insertar elemento aleatorio");
		insertRandomButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
		insertRandomButton.setMnemonic(KeyEvent.VK_R);
		insertRandomButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						insertRandomButtonActionPerformed(evt);
					}
				});

		parameterLabel = new JLabel();
		parameterCombo = new JComboBox();
		parameterCombo.putClientProperty("JComponent.sizeVariant", "large");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(textField,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										45,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(17, 17, 17)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(insertButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(removeButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(insertRandomButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(clearButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(17)
								.addComponent(traverseButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(traverseOption,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										109,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										21, Short.MAX_VALUE)
								.addComponent(parameterLabel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(parameterCombo,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(9, 9, 9)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(3, 3, 3)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														textField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1)
												.addComponent(
														insertButton,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														insertRandomButton,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														removeButton,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														traverseButton,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														traverseOption,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														clearButton,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														parameterLabel,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														parameterCombo,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
				/*
				 * .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
				 * Short.MAX_VALUE)
				 */));
	}// </editor-fold>//GEN-END:initComponents

	private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_insertButtonActionPerformed
		String text = getText();
		try {
			if (text == null || text.isEmpty()) {
				controller
						.showLogMessage("ERROR: El valor ingresado no debe ser vacío.");
			} else {
				controller.insert(text);
			}
		} finally {
			textField.setText("");
		}
	}// GEN-LAST:event_insertButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_removeButtonActionPerformed
		String text = getText();
		try {
			if (text == null || text.isEmpty()) {
				controller
						.showLogMessage("ERROR: El valor ingresado no debe ser vacío.");
			} else {
				controller.remove(text);
			}
		} finally {
			textField.setText("");
		}
	}// GEN-LAST:event_removeButtonActionPerformed

	private void traverseButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_traverseButtonActionPerformed
		switch (traverseOption.getSelectedIndex()) {
		case 0:
			controller.traverseInOrder();
			break;
		case 1:
			controller.traversePreOrder();
			break;
		case 2:
			controller.traversePostOrder();
			break;
		}
	}// GEN-LAST:event_traverseButtonActionPerformed

	private void parameterButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_parameterButtonActionPerformed
		int parameter = (Integer) parameterCombo.getSelectedItem();
		controller.changeParameter(parameter);
	}// GEN-LAST:event_parameterButtonActionPerformed

	private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_clearButtonActionPerformed
		controller.clear();
	}// GEN-LAST:event_clearButtonActionPerformed

	private void insertRandomButtonActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_insertRandomButtonActionPerformed
		controller.insertRandom();
	}// GEN-LAST:event_insertRandomButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton clearButton;
	private JButton insertButton;
	private JButton insertRandomButton;
	private JLabel parameterLabel;
	private JComboBox parameterCombo;
	private JButton removeButton;
	private JTextField textField;
	private JButton traverseButton;
	private JComboBox traverseOption;
	private JLabel jLabel1;
	// End of variables declaration//GEN-END:variables
}
